"""
Case Type   : 事件触发器
Case Name   : 【事件触发器】修改事件触发器，禁用指定触发器
Create At   :   2024/07/16
Owner       :   sungang14
Description :
    预置条件：
        1.创建事件触发器函数：
        CREATE OR REPLACE FUNCTION f_eventtrigger_0055()
        RETURNS event_trigger
        LANGUAGE plpgsql
        AS $$ BEGIN
        RAISE NOTICE 'test_event_trigger: % %', tg_event, tg_tag;
        END;
        $$;
        2.创建事件触发器
        CREATE EVENT TRIGGER eventtri_eventtrigger_0055 ON ddl_command_start EXECUTE PROCEDURE f_eventtrigger_0055();
    测试步骤：
        1.禁用触发器
        ALTER EVENT TRIGGER eventtri_eventtrigger_0055 DISABLE;
        2.执行创建表触发事件触发器
        CREATE TABLE t_eventtrigger_0055(id int,col varchar2(20));
        3.再次禁用触发器
        ALTER EVENT TRIGGER eventtri_eventtrigger_0055 DISABLE;
        4、查询事件触发器enable状态
        select evtenabled from pg_event_trigger where evtname = 'eventtri_eventtrigger_0055';
        \dy eventtri_eventtrigger_0055查看回显Enabled字段
Expect      : 
        1.修改成功
        2.执行创建表不会触发事件触发器
        3、禁用成功
        4、pg_event_trigger表evtenabled字段显示D；\dy对应事件触发器显示disabled
History     : 
"""

import sys
import os
import unittest
from yat.test import Node
from yat.test import macro

sys.path.append(sys.path[0] + "/../")
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH


class IndexFileDamaged(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f'-----{os.path.basename(__file__)} start-----')
        self.logger.info('----------------this is setup-----------------------')
        self.DB_ENV_PATH = macro.DB_ENV_PATH
        self.DB_INSTANCE_PATH = macro.DB_INSTANCE_PATH
        self.comsh = CommonSH('PrimaryDbBmode')
        self.userNode = self.comsh.node
        self.comsh.execut_db_sql("drop EVENT TRIGGER if exists eventtri_eventtrigger_0055;")
        self.comsh.execut_db_sql("drop function if exists f_eventtrigger_0055();")
        self.comsh.execut_db_sql("drop table if exists t_eventtrigger_0055;")
        msg = self.comsh.execut_db_sql(
            f"CREATE OR REPLACE FUNCTION f_eventtrigger_0055() RETURNS event_trigger LANGUAGE plpgsql" \
            f" AS \$\$ BEGIN" \
            f" RAISE NOTICE 'test_event_trigger: % %', tg_event, tg_tag;" \
            f"END;" \
            f"\$\$;")
        self.logger.info(msg)
        self.assertIn("CREATE FUNCTION", msg)
        msg = self.comsh.execut_db_sql(
            "CREATE EVENT TRIGGER eventtri_eventtrigger_0055 ON ddl_command_start EXECUTE PROCEDURE f_eventtrigger_0055();")
        self.logger.info(msg)
        self.assertEqual(f'CREATE EVENT TRIGGER', msg)

    def test_Index_file_damaged(self):
        self.logger.info("--step1:禁用触发器")
        msg = self.comsh.execut_db_sql("ALTER EVENT TRIGGER eventtri_eventtrigger_0055 DISABLE;")
        self.logger.info(msg)
        self.assertEqual(f'ALTER EVENT TRIGGER', msg)
        self.logger.info("--step2:执行创建表触发事件触发器")
        msg = self.comsh.execut_db_sql("CREATE TABLE t_eventtrigger_0055(id int,col varchar2(20));")
        self.logger.info(msg)
        self.assertIn(f'CREATE TABLE', msg)
        self.logger.info("--step3:再次禁用触发器")
        msg = self.comsh.execut_db_sql("ALTER EVENT TRIGGER eventtri_eventtrigger_0055 DISABLE;")
        self.logger.info(msg)
        self.assertEqual(f'ALTER EVENT TRIGGER', msg)
        self.logger.info("--step4:查询事件触发器enable状态")
        msg = self.comsh.execut_db_sql(
            "select evtenabled from pg_event_trigger where evtname = 'eventtri_eventtrigger_0055';")
        self.logger.info(msg)
        self.assertIn(f'evtenabled \n------------\n D', msg)
        msg = self.comsh.execut_db_sql("\dy eventtri_eventtrigger_0055")
        self.logger.info(msg)
        self.assertIn(
            f"eventtri_eventtrigger_0055 | ddl_command_start | {self.userNode.ssh_user} | disabled | f_eventtrigger_0055",
            msg)

    def tearDown(self):
        self.logger.info('----------------this is tearDown-----------------------')
        # 删除事件触发器
        msg = self.comsh.execut_db_sql("drop EVENT TRIGGER if exists eventtri_eventtrigger_0055;")
        self.logger.info(msg)
        self.assertIn('DROP EVENT TRIGGER', msg)
        # 删除事件触发器函数
        msg = self.comsh.execut_db_sql("drop function if exists f_eventtrigger_0055();")
        self.logger.info(msg)
        self.assertIn('DROP FUNCTION', msg)
        # 删除表
        msg = self.comsh.execut_db_sql("drop table if exists t_eventtrigger_0055;")
        self.logger.info(msg)
        self.assertEqual('DROP TABLE', msg)
        self.logger.info(f'-----{os.path.basename(__file__)} end-----')